/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-06-19
 * Time: 23:17
 */
//力扣：https://leetcode.cn/problems/middle-of-the-linked-list/submissions/637892295/
//求中间节点
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
public class Test {
    public static ListNode middleNode(ListNode head) {
        if(head==null){
            return head;
        }
        ListNode p1=head;
        ListNode p2=head;
        while(p2!=null&&p2.next!=null){
            p1=p1.next;
            p2=p2.next.next;
        }
        return p1;
    }

    public static void main(String[] args) {
        ListNode list1=new ListNode(1);
        ListNode list2=new ListNode(2);
        ListNode list3=new ListNode(3);
        ListNode list4=new ListNode(4);
        ListNode list5=new ListNode(5);
        list1.next=list2;
        list2.next=list3;
        list3.next=list4;
        list4.next=null;
        System.out.println(middleNode(list1).val);
        list4.next=list5;
        list5.next=null;
        System.out.println(middleNode(list1).val);
    }
}
